
package com.dstz.form.util;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.TreeSet;
import java.util.function.Function;
import java.util.stream.Collectors;

import org.apache.commons.lang3.ArrayUtils;
import org.springframework.util.CollectionUtils;

import com.dstz.form.vo.SqlButtonVO;

/**
 * 数组相关的帮助类
 * <p>
 * 可以先到ArrayUtils（cpmmoms-lang-*.jar）找
 * </p>
 *
 */
public class ArrayUtil {
	
	/**
	 * 字符串数组转换为Long数组
	 * 
	 * @param aryStr 字符串数组
	 * @return Long数组
	 **/
	public static Long[] convertArray(String[] aryStr) {
		if (ArrayUtils.isEmpty(aryStr))
			return ArrayUtils.EMPTY_LONG_OBJECT_ARRAY;
		Long[] aryLong = new Long[aryStr.length];
		for (int i = 0; i < aryStr.length; i++) {
			aryLong[i] = Long.parseLong(aryStr[i]);
		}
		return aryLong;
	}
	
	/**
	 * 字符串数组转换为Long数组
	 * 
	 * @param aryStr 字符串数组
	 * @return Long数组
	 **/
	public static String[] toStringArray(List<String> list) {
		
		if (CollectionUtils.isEmpty(list)) {
			return null;
		}
		String[] strs = new String[list.size()];
		for (int i = 0; i < list.size(); i++) {
			strs[i] = list.get(i);
		}
		return strs;
	}
	
	/**
	 * Long数组转换为字符串数组
	 * 
	 * @param aryLong Long数组
	 * @return 字符串数组
	 **/
	public static String[] convertArray(Long[] aryLong) {
		if (ArrayUtils.isEmpty(aryLong))
			return ArrayUtils.EMPTY_STRING_ARRAY;
		String[] aryStr = new String[aryLong.length];
		for (int i = 0; i < aryStr.length; i++) {
			aryStr[i] = String.valueOf(aryStr[i]);
		}
		return aryStr;
	}
	
	/**
	 * 拼装数组。
	 * 
	 * @param aryStr
	 * @param separator
	 * @return
	 **/
	public static String contact(String[] aryStr, String separator) {
		if (aryStr.length == 1)
			return aryStr[0];
		String out = "";
		for (int i = 0; i < aryStr.length; i++) {
			if (i == 0) {
				out += aryStr[i];
			} else {
				out += separator + aryStr[i];
			}
		}
		return out;
	}
	
	/**
	 * 添加引号。
	 * 例如 1,2,3 修改为 '1','2','3'.
	 * 
	 * @param val
	 * @return
	 **/
	public static String addQuote(String val) {
		String[] aryVal = val.split(",");
		
		if (aryVal.length == 1)
			return "'" + val + "'";
		
		String tmp = "";
		for (int i = 0; i < aryVal.length; i++) {
			if (i == 0) {
				tmp += "'" + aryVal[i] + "'";
			} else {
				tmp += ",'" + aryVal[i] + "'";
			}
		}
		return tmp;
	}
	
	/**
	 * list去重
	 * 
	 * @param list
	 * @return
	 * @return List<String>
	 * @author xianggang
	 * @date 2016年10月9日 下午8:29:58
	 **/
	public static List<String> getUniqueList(List<String> list) {
		List<String> tempList = new ArrayList<String>();
		for (String i : list) {
			if (!tempList.contains(i)) {
				tempList.add(i);
			}
		}
		return tempList;
	}
	
	/**
	 * 查看数组中是否包含某个元素
	 * 
	 * @param arr
	 * @param targetValue
	 * @return
	 * @return boolean
	 * @author xianggang
	 * @date 2016年10月12日 下午3:06:49
	 **/
	public static boolean contains(int[] arr, int targetValue) {
		return Arrays.asList(arr).contains(targetValue);
	}
	
	public static String join(Object[] o, String flag) {
		StringBuffer str_buff = new StringBuffer();
		
		for (int i = 0, len = o.length; i < len; i++) {
			str_buff.append(String.valueOf(o[i]));
			if (i < len - 1)
				str_buff.append(flag);
		}
		
		String str = str_buff.toString();
		if (flag.startsWith("'")) {
			str = "'" + str + "'";
		}
		if (flag.startsWith("\"")) {
			str = "\\\"" + str + "\\\"";
		}
		
		return str;
	}
	
	public static String join(List<String> o, String flag) {
		StringBuffer str_buff = new StringBuffer();
		
		for (int i = 0, len = o.size(); i < len; i++) {
			str_buff.append(String.valueOf(o.get(i)));
			if (i < len - 1)
				str_buff.append(flag);
		}
		
		return str_buff.toString();
	}
	
	/**
	 * 
	 * @param arrays
	 * @param cntype
	 * @return
	 * @return boolean
	 * @author as
	 * @date 2018年7月25日 上午11:36:33
	 */
	public static boolean contains(String[] arrays, String cntype) {
		if (arrays == null) {
			return false;
		}
		for (String string : arrays) {
			if (string.equals(cntype)) {
				return true;
			}
		}
		return false;
	}
	
}
